package com.drone.drone_server.controller;

import com.drone.drone_server.api.ApiResponse;
import com.drone.drone_server.dao.*;
import com.drone.drone_server.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/actions")
class ActionController {

    @Autowired
    private DroneActionDao droneActionDao;

    // 新建行为
    @PostMapping("/createAction")
    public ApiResponse createAction(@RequestBody DroneAction action) {
        action.setDroneActionId(null);
        int result = droneActionDao.insert(action);
        return result > 0 ? ApiResponse.success("行为创建成功") : ApiResponse.fail("行为创建失败");
    }

    // 开启/关闭行为
    @PutMapping("/toggleActionStatus")
    public ApiResponse toggleActionStatus(@RequestParam Integer actionId,
                                          @RequestParam Boolean enabled) {
        DroneAction action = droneActionDao.selectById(actionId);
        if (action == null) {
            return ApiResponse.fail("行为不存在");
        }
        action.setEnabled(enabled);
        int result = droneActionDao.updateById(action);
        return result > 0 ? ApiResponse.success("状态更新成功") : ApiResponse.fail("状态更新失败");
    }

    // 修改行为
    @PostMapping("/updateAction")
    public ApiResponse updateAction(@RequestBody DroneAction action) {
        int result = droneActionDao.updateById(action);
        return result > 0 ? ApiResponse.success("行为更新成功") : ApiResponse.fail("行为更新失败");
    }

    // 删除行为
    @DeleteMapping("/deleteAction")
    public ApiResponse deleteAction(@RequestParam Integer actionId) {
        int result = droneActionDao.deleteById(actionId);
        return result > 0 ? ApiResponse.success("行为删除成功") : ApiResponse.fail("行为删除失败");
    }

    @GetMapping("/getAllDroneAction")
    public ApiResponse getAllDroneAction(){
        return ApiResponse.success(droneActionDao.selectAllDroneActionAndRoute());
    }
}
